package com.data_structure_algorithm.data_structure.tree.bin_tree;

/**
 * @Authror PABLO
 * @Date 2022/3/30 20:14
 * @Desc 线索化二叉树
 */


public class ThreadedBinaryTree {
    public static void main(String[] args) {
        HreoNode root = new HreoNode(1, "宋江");
        HreoNode node2 = new HreoNode(3, "吴用");
        HreoNode node3 = new HreoNode(6, "林冲");
        HreoNode node4 = new HreoNode(8, "呼延灼");
        HreoNode node5 = new HreoNode(10, "柴进");
        HreoNode node6 = new HreoNode(14, "武松");

        root.setLeft(node2);
        root.setRight(node3);
        node2.setLeft(node4);
        node2.setRight(node5);
        node3.setLeft(node6);

        ThreadedBinaryTreeClass t = new ThreadedBinaryTreeClass();
        t.setRoot(root);
        t.threadedNode();
        //中序遍历
        t.midOrder();
     /*   //测试
        HreoNode left = node5.getLeft();
        HreoNode right = node5.getRight();
        System.out.println(left);
        System.out.println(right);*/


    }
}

//线索化二叉树类
class ThreadedBinaryTreeClass {
    private HreoNode root;
    //前驱节点
    //递归线索化时，该节点总是保留当前节点的前节点
    private HreoNode preNode;

    //重载线索化方法，表示当前从root节点开始往下找
    public void threadedNode() {
        this.threadedNode(root);
    }

    //二叉树中序线索化
    //node为当前需要线索化的节点
    public void threadedNode(HreoNode node) {
        if (node == null) return;
        //线索化左子树
        threadedNode(node.getLeft());

        //---------------------------------------------------------------------------
        //处理前驱节点 线索化当前节点
        //中序情况下，如果left指针可能指向左子树/前驱节点，如果left为null，说明该节点是叶子结点
        if (node.getLeft() == null) {
            //指定前驱节点是上一个节点，如当前节点是左下角节点，前驱节点是默认值null
            node.setLeft(preNode);
            //修改左指针类型指向前驱
            node.setLeftType(1);//标记

        }
        //处理后继节点  想想左下角的叶子结点  pre节点相当于在这个叶子结点前面的空节点，该pre节点的前驱为null,后继为这个叶子结点
        if (preNode != null && preNode.getRight() == null) {
            //前驱节点的右指针指向当前节点
            preNode.setRight(node);
            preNode.setRightType(1);//标记
        }
        //每处理一个节点后，当前节点变为下个节点的前驱节点，这里是为下次递归做前驱节点初始化
        preNode = node;

        //---------------------------------------------------------------------------
        //线索化右子树
        threadedNode(node.getRight());
    }


    public HreoNode getRoot() {
        return root;
    }

    public void setRoot(HreoNode root) {
        this.root = root;
    }

    //中序遍历
    //注意：线索化后的二叉树不能简单遍历，要过滤叶子结点的线索
    public void midOrder() {
        this.midOrder(root);

    }

    public void midOrder(HreoNode root) {
        if (root == null) return;
        //中序 左中右
        if (root.getLeftType() != 1) {
            //说明不是线索节点
            midOrder(root.getLeft());
        }
        //走到这里为左子节点
        System.out.println(root);
        if (root.getRightType() != 1) {
            //说明不是线索节点
            midOrder(root.getRight());
        }

    }
}

class HreoNode {
    private int no;
    private String name;
    //默认null
    private HreoNode left;
    //默认null
    private HreoNode right;

    //left类型   0左子树   1前驱
    private int leftType;
    //right类型   0左子树   1前驱
    private int rightType;


    public HreoNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public HreoNode getLeft() {
        return left;
    }

    public void setLeft(HreoNode left) {
        this.left = left;
    }

    public HreoNode getRight() {
        return right;
    }

    public void setRight(HreoNode right) {
        this.right = right;
    }

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    @Override
    public String toString() {
        return "HreoNode{" +
                "no=" + no +
                ", name.'" + name + '\'' +
                '}';
    }
}